﻿using Microsoft.AspNetCore.Mvc;
using ProjectScheduling_web.model;
using ProjectScheduling_web.pagemodel;
using SqlSugar;
using System.Linq.Expressions;

namespace ProjectScheduling_web.dao
{
    public class ProjectsDao : BaseDao<Projects>
    {
        public static readonly ProjectsDao Base = new();
        /// <summary>
        /// 显示最近N条数据
        /// </summary>
        /// <param name="func"></param>
        /// <param name="Count"></param>
        /// <returns></returns>
        public List<Projects> FindAll(Expression<Func<Projects, bool>> func, int Count)
        {
            using var db = new SqlSugarClient(ConnectionConfig);
            return db.Queryable<Projects>().Where(func).OrderByDescending(it => it.Id).Take(Count).Includes(it => it.ProjectType).Includes(it => it.SaleMsg).Includes(it => it.StaffsList).Includes(it => it.PreparedDatas).ToList();
        }
        public new PageData<Projects> FindAll(int page, int pageSize, Expression<Func<Projects, bool>> func)
        {
            using var db = new SqlSugarClient(ConnectionConfig);
            var list = db.Queryable<Projects>().Where(func).OrderByDescending(it => it.CreateDate).
                        Includes(it => it.ProjectType).Includes(it => it.SaleMsg).Includes(it => it.StaffsList).
                        Includes(it => it.PreparedDatas).Skip((page - 1) * pageSize).Take(pageSize).ToList();
            foreach (var item in list)
            {
                if (item.PreparedDatas != null)
                {
                    var dlst = PreparedDataDao.Base.FindAll(it => it.ProjectId == item.Id);
                    item.StaffDays = dlst.Sum(it => it.PrepardStaffs?.Sum(item => item.Days)) ?? 0;
                }
            }
            return new PageData<Projects>
            {
                Items = list,
                Total = db.Queryable<Projects>().Where(func).Count()
            };
        }
        public new List<Projects> FindAll(Expression<Func<Projects, bool>> func)
        {
            using var db = new SqlSugarClient(ConnectionConfig);
            return db.Queryable<Projects>().Where(func).OrderByDescending(it => it.CreateDate).Includes(it => it.ProjectType).Includes(it => it.SaleMsg).Includes(it => it.StaffsList).Includes(it => it.PreparedDatas).ToList();
        }
        /// <summary>
        /// 获取当前用户的按类型分组信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>

        public List<ProjectGroupByTypeModel> GroupMsg(int userId)
        {
            using var db = new SqlSugarClient(ConnectionConfig);
            var types = db.Queryable<ProjectTypes>().ToList();
            var datas = db.Queryable<Projects>().
                //Includes(it => it.ProjectType).
                Where(it => (userId == 0 || it.SalespersonId == userId)).
                GroupByIF(true, "ProjectTypeId").
                Select(it => new ProjectGroupByTypeModel { Id = it.ProjectTypeId, Count = SqlFunc.AggregateCount(it) })
                .ToList().Select(it =>
                {
                    var obj = types.Find(z => z.Id == it.Id);
                    return new ProjectGroupByTypeModel
                    {
                        Id = it.Id,
                        Name = obj?.Name,
                        Count = it.Count
                    };
                }).ToList();
            return datas;
        }
    }
}
